@extends('Layout/personBase')

@section('title', '密码修改')

@section('css')

	<link href="{{asset('Home/css/stepstyle.css')}}" rel="stylesheet" type="text/css">

@endsection


@section('right')

	<div class="am-cf am-padding">
		<div class="am-fl am-cf"><strong class="am-text-danger am-text-lg">修改密码</strong> / <small>Password</small></div>
	</div>
	<hr/>
	<!--进度条-->
	
	<form class="am-form am-form-horizontal">
		<div class="am-form-group">
			<label for="user-old-password" class="am-form-label">原密码</label>
			<div class="am-form-content old">
				<input type="password" id="user-old-password" placeholder="请输入原登录密码">
			</div>
		</div>
		<div class="am-form-group">
			<label for="user-new-password" class="am-form-label">新密码</label>
			<div class="am-form-content new">
				<input type="password" id="user-new-password" placeholder="由数字、字母和特殊符号组合">
			</div>
		</div>
		<div class="am-form-group">
			<label for="user-confirm-password" class="am-form-label">确认密码</label>
			<div class="am-form-content confirm">
				<input type="password" id="user-confirm-password" placeholder="请再次输入上面的密码">
			</div>
		</div>
		<div class="info-btn">
			<div class="am-btn am-btn-danger modify">保存修改</div>
		</div>

	</form>
			
@endsection

@section('bottom-js')
	<script type="text/javascript">
		//$('.modify').attr('name', 'modify');
		
		$("#user-new-password").attr("disabled","disabled");
		$("#user-confirm-password").attr("disabled","disabled");

		$('#user-old-password').blur(function () { 

			var pass = $('#user-old-password').val();

			var token  = "{{csrf_token()}}";
			//alert(pass);
			$.ajax({ 
				url: "{{url('/Home/oldPass')}}",
				type: 'post',
				data: {'pass':pass, '_token':token},
				dataType: 'json',
				success:function (data) {
					//console.log(data.code);
					if (data.code == 1200) { 
						$("p[name='old']").remove();
						$('.old').append("<p name='old'>"+data.msg+"</p>").css('color', 'green');
						$("#user-new-password").removeAttr("disabled","disabled");
						
					}else{ 
						$('.modify').removeAttr('name', 'modify');
						$("p[name='old']").remove();
						$('.old').append("<p name='old'>"+data.msg+"</p>").css('color', 'red');
						$("#user-new-password").attr("disabled","disabled");
						$("#user-confirm-password").attr("disabled","disabled");

					}
	            },
	            

			})
		})

		$('#user-new-password').blur(function () { 

			var npass = $('#user-new-password').val();
			var token  = "{{csrf_token()}}";
			if (npass == false) { 
				$('.modify').removeAttr('name', 'modify');
				$("p[name='new']").remove();
				$('.new').append("<p name='new'>新密码不能为空</p>").css('color', 'red');
				
				$("#user-confirm-password").attr("disabled","disabled");
						
			}else {
				$.ajax({ 
					url: "{{url('/Home/newPass')}}",
					type: 'post',
					data: {'npass':npass, '_token':token},
					dataType: 'json',
					success:function (data) {
						console.log(data);
						if (data.code == 1301) { 
							$("p[name='new']").remove();
							$('.new').append("<p name='new'>"+data.msg+"</p>").css('color', 'green');
							
							$("#user-confirm-password").removeAttr("disabled","disabled");
						}else{ 
							$('.modify').removeAttr('name', 'modify');
							$("p[name='new']").remove();
							$('.new').append("<p name='new'>"+data.msg+"</p>").css('color', 'red');
							$("#user-confirm-password").attr("disabled","disabled");

						}
		            },
				})
			}
		});
		$('#user-confirm-password').blur(function () { 

			var npass = $('#user-new-password').val();
			var cpass = $('#user-confirm-password').val();
			if (npass == cpass ) { 
				$('.modify').removeAttr('name', 'modify');
				$("p[name='confirm']").remove();
				$('.confirm').append("<p name='confirm'>密码正确</p>").css('color', 'green');
				$('.modify').attr('name', 'modify');
				//console.log($('.modify').attr('name', 'modify'));
				
			}else{ 
				$('.modify').removeAttr('name', 'modify');
				$("p[name='confirm']").remove();
				$('.confirm').append("<p name='confirm'>与新密码不一致</p>").css('color', 'red');
			}

		})
		//console.log($('div[name="modify"]'));
		$('.modify').click(function () { 
			var mod = $('div[name="modify"]')
			if (mod !== false) { 
				
			
				var opass = $('#user-old-password').val();
				var npass = $('#user-new-password').val();
				var cpass = $('#user-confirm-password').val();
				var token  = "{{csrf_token()}}";
				$.ajax({ 
					url: "{{url('/Home/modify')}}",
					type: 'post',
					data: {'opass':opass, 'npass':npass, 'cpass':cpass, '_token':token},
					dataType: 'json',
					success:function (data) {
						console.log(data);
						if (data.code == 2400) { 
							alert(data.msg);
							location.href = "{{url('/logout')}}";
						}else{ 
							alert(data.msg);
							location.href = "{{url('/Home/passChange')}}";
						}
		            },
				})
			}
		})
		
	</script>
@endsection